'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('outfit_items', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      outfit_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '搭配ID',
        references: {
          model: 'outfits',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      clothing_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '衣服ID',
        references: {
          model: 'clothing',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      position: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '位置(top, bottom, shoes, accessories等)',
      },
      layer: {
        type: Sequelize.INTEGER,
        defaultValue: 1,
        comment: '层级(1=内层, 2=中层, 3=外层)',
      },
      is_main: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: '是否为主要单品',
      },
      notes: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '备注',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 添加索引
    await queryInterface.addIndex('outfit_items', ['outfit_id']);
    await queryInterface.addIndex('outfit_items', ['clothing_id']);
    await queryInterface.addIndex('outfit_items', {
      fields: ['outfit_id', 'clothing_id'],
      unique: true,
      name: 'unique_outfit_clothing',
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('outfit_items');
  },
};
